﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccessLibrary.DAL;
using System.Data.Linq;
using entities;

namespace BusinessLogicLibrary
{
    public class RoomModule
    {
        private readonly RoomDAO _roomDao = new RoomDAO();

        public bool AddEquipmentDetail(string name, decimal price, bool available)
        {
            if (name.Trim() != "")
            {
                var equipmentDetail = new EquipmentDetail
                                          {
                                              Available = available,
                                              EquipmentPrice = price,
                                              EquipmentName = name
                                          };
                return _roomDao.InsertEquipmentDetail(equipmentDetail);
            }
            throw new Exception("Invalid data");
        }

        public List<EquipmentDetail> GetEquipmentDetails(bool available)
        {
            return available ? _roomDao.SelectEquipmentsDetails(1) : _roomDao.SelectEquipmentsDetails(-1);
        }

        public EquipmentDetail GetEquipmentDetail(int equipmentId)
        {
            return _roomDao.SelectEquipmentDetail(equipmentId);
        }

        public List<EquipmentDetail> GetEquipmentDetails(string equipmentName)
        {
            return _roomDao.SelectEquipmentsDetails(equipmentName);
        }

        public List<EquipmentDetail> GetEquipmentDetailsByRoomTypeID(int roomTypeId)
        {
            return _roomDao.SelectEquipmentDetailsByRoomTypeID(roomTypeId);
        }

        public bool ModifyEquipmentDetail(int id, string name, decimal price, bool available)
        {
            if (name.Trim() != "")
            {
                var equipmentDetail = new EquipmentDetail
                                          {
                                              EquipmentDetailID = id,
                                              Available = available,
                                              EquipmentName = name,
                                              EquipmentPrice = price
                                          };
                return _roomDao.UpdateEquipmentDetail(equipmentDetail);
            }
            throw new Exception("Invalid data");
        }

        public RoomType GetRoomType(int roomTypeId)
        {
            return _roomDao.SelectRoomType(roomTypeId);
        }

        public List<RoomType> GetRoomTypes()
        {
            return _roomDao.SelectRoomType();
        }

        public bool AddRoomType(string title, string rank, decimal price, bool bath, bool view, bool balcon,
                                List<int> equipmentDetailIDList)
        {
            if (title.Trim() != "")
            {
               var roomType = new RoomType
                                   {
                                       RoomTypeTitle = title,
                                       RoomRank = rank,
                                       RoomPrice = price,
                                       Balcony = balcon,
                                       Bath = bath,
                                       View = view
                                   };
               return _roomDao.InsertRoomType(roomType, equipmentDetailIDList);
            }
            throw new Exception("Invalid data");
        }

        public bool ModifyRoomType(int id, string title, string rank, decimal price, bool bath, bool view,
                                   bool balcony, IEnumerable<int> equipmentDetailIds)
        {
            if (title.Trim() != "" && rank.Trim() != "")
            {
               var roomType = new RoomType
                                   {
                                       RoomTypeID = id,
                                       RoomTypeTitle = title,
                                       RoomRank = rank,
                                       RoomPrice = price,
                                       Bath = bath,
                                       View = view,
                                       Balcony = balcony
                                   };
               return _roomDao.UpdateRoomType(roomType, equipmentDetailIds);
            }
            throw new Exception("Invalid data");
        }

        public List<Room> GetRooms(int available)
        {
            return _roomDao.SelectRooms(available);
        }

        public bool AddRoom(int roomId, int status, int roomTypeId)
        {
            if (status > 1 || status < -1)
            {
                throw new Exception("Invalid data");
            }
            var room = new Room
                           {
                               RoomID = roomId,
                               Status = status,
                               RoomTypeID = roomTypeId
                           };
            return _roomDao.InsertRoom(room);
        }

        public Room GetRoom(int roomId)
        {
            return _roomDao.SelectRoom(roomId);
        }

        public List<Room> GetRoomsByRoomTypeID(int roomTypeId)
        {
            return _roomDao.SelectRoomsByRoomTypeID(roomTypeId);
        }

        public bool ModifyRoom(int roomId, int status, int roomTypeId)
        {
            if (status > 1 || status < -1)
            {
                throw new Exception("Invalid data");
            }
            var room = new Room
                           {
                               RoomID = roomId,
                               Status = status,
                               RoomTypeID = roomTypeId
                           };
            return _roomDao.UpdateRoom(room);
        }
    }
}